package com.ruoyi.expressdelivery.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.expressdelivery.domain.ExpressDelivery;
import com.ruoyi.expressdelivery.mapper.ExpreeDeliveryMapper;
import com.ruoyi.expressdelivery.service.ExpressDeliveryService;
import com.ruoyi.expressdelivery.utils.CallExpressServiceTools;
import com.ruoyi.expressdelivery.utils.ExpressServiceCodeEnum;
import com.ruoyi.expressdelivery.utils.HttpClientUtil;
import com.ruoyi.expressdelivery.utils.IServiceCodeStandard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("business/expressDelivery")
public class ExpressDeliveryController {

    // 面单打印的的编码 private static final String CLIENT_CODE = "LJTN5MUYV3";  //此处替换为您在丰桥平台获取的顾客编码
    //  面单打印的的编码  private static final String CHECK_WORD = "ka9vf65rlEroiaL9UogiQnZufrgh8dq2";//此处替换为您在丰桥平台获取的校验码
    //查询订单的编码 cI9xGJrCVMRdWDnyDShZDaBId3S74FZr
    private static final String CLIENT_CODE = "LJTDMI8PUE";  // 下单接口的编码
    private static final String CHECK_WORD = "ThbsndrbMWDEOAU1RBRyiekL8ozF8LuV"; // 下单接口的编码
    @Autowired
    ExpressDeliveryService expressDeliveryService;
    @Autowired
    ExpreeDeliveryMapper expreeDeliveryMapper;

    public static void main(String[] args) throws UnsupportedEncodingException {

        String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";
        IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_CREATE_ORDER; //下订单
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ORDER_RESP; //查询订单是否下单成功 校验码 6qu8zGhrh56suLubQcDc7sWWgUXeJhOR
        //  IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_UPDATE_ORDER;//下订单取消接口
        // 	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_FILTER_ORDER_BSP;//订单筛选
        //  IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;//查路由 WTmE1nqvzhQqtsthmU5NkbNdqv8DQKFu LJT9MX99Y1
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_GET_SUB_MAILNO;//子单号
        //  	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_QUERY_SFWAYBILL;//查运费 LJTMOIR1Z5 5H8RFwhPylMSLOfPqC8reaKToMa5PFmr
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_REGISTER_ROUTE;//注册路由
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_CREATE_REVERSE_ORDER;//退货下单
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_CANCEL_REVERSE_ORDER;//退货消单
        //	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_WANTED_INTERCEPT;//截单转寄
        // 	IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_QUERY_DELIVERTM;//时效标准及价格查询
        //IServiceCodeStandard standardService = ExpressServiceCodeEnum.COM_RECE_CLOUD_PRINT_WAYBILLS;//面单打印
        CallExpressServiceTools tools = CallExpressServiceTools.getInstance();

        // set common header
        Map<String, String> params = new HashMap<String, String>();

        String timeStamp = String.valueOf(System.currentTimeMillis());
        String msgData = "{\n" +
                "    \"cargoDetails\":[\n" +
                "        {          \n" +
                "            \"count\":2.365,\n" +
                "\t\t\t\"unit\":\"个\",\n" +
                "\t\t\t\"weight\":6.1,\n" +
                "\t\t\t\"amount\":100.5111,\n" +
                "            \"currency\":\"HKD\",\n" +
                "            \"name\":\"护肤品1\",           \n" +
                "            \"sourceArea\":\"CHN\"          \n" +
                "        }],\n" +
                "    \"contactInfoList\":\t[\n" +
                "        {\n" +
                "            \"address\":\"济南市天桥区泉舜小区\",\n" +
                "            \"contact\":\"小曾\",\n" +
                "            \"contactType\":1,\n" +
                "            \"country\":\"CN\",\n" +
                "            \"postCode\":\"580058\",\n" +
                "            \"tel\":\"4006789888\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"address\":\"广东省广州市白云区湖北大厦\",\n" +
                "            \"company\":\"顺丰速运\",\n" +
                "            \"contact\":\"小邱\",\n" +
                "            \"contactType\":2,\n" +
                "            \"country\":\"CN\",\n" +
                "            \"postCode\":\"580058\",\n" +
                "            \"tel\":\"18688806057\"\n" +
                "        }],\n" +
                "    \"language\":\"zh_CN\",\n" +
                "    \"orderId\":\"OrderNua20200612565\"\n" +
                "}";
     /*   String msgData ="{\n" +
                "\t\"templateCode\": \"fm_150_standard_LJTN5MUYV3\",\n" +
                "\t\"fileType\": \"pdf\",\n" +
                "\t\"documents\": [{\n" +
                "\t\t\"masterWaybillNo\": \"SF6026001464668\",\n" +
                "\t\t\"branchWaybillNo\": \"\",\n" +
                "\t\t\"seq\": 1,\n" +
                "\t\t\"sum\": \"\",\n" +
                "\t\t\"pickupEmpNo\": \"\",\n" +
                "\t\t\"systemSource\": \"OWF\",\n" +
                "\t\t\"isPrintLogo\": \"true\",\n" +
                "\t\t\"printNum\": \"\",\n" +
                "\t\t\"printDateTime\": \"2021年1月22日上午9點49\",\n" +
                "\t\t\"agingType\": \"T4\",\n" +
                "\t\t\"destRouteLabel\": \"852\",\n" +
                "\t\t\"fromName\": \"張冰滔\",\n" +
                "\t\t\"fromPhone\": \" 15786456734\",\n" +
                "\t\t\"fromAddress\": \"香港葵青區青衣香港圖書文具有限公司\",\n" +
                "\t\t\"fromOrgName\": \"\",\n" +
                "\t\t\"toName\": \"張冰滔\",\n" +
                "\t\t\"toPhone\": \" 15786456734\",\n" +
                "\t\t\"toOrgName\": \"\",\n" +
                "\t\t\"toAddress\": \"香港葵青區青衣香港圖書文具有限公司\",\n" +
                "\t\t\"destTeamCode\": \"\",\n" +
                "\t\t\"isCod\": \"false\",\n" +
                "\t\t\"payment\": \"寄付现结\",\n" +
                "\t\t\"codingMapping\": \"\",\n" +
                "\t\t\"twoDimensionCode\": \"MMM={'k1':'852','k2':'852','k3':'','k4':'T4','k5':'SF6026001464668','k6':'','k7':'b0cf9219'}\",\n" +
                "\t\t\"abFlag\": \"\",\n" +
                "\t\t\"incrementService\": \"\",\n" +
                "\t\t\"codingMappingOut\": \"\",\n" +
                "\t\t\"entrustedArticles\": \"\",\n" +
                "\t\t\"chargedWeight\": 0.5,\n" +
                "\t\t\"actualWeight\": \"\",\n" +
                "\t\t\"costTotal\": 22,\n" +
                "\t\t\"remark\": \"\",\n" +
                "\t\t\"printIcons\": [\n" +
                "\t\t\t\"标快\"\n" +
                "\t\t],\n" +
                "\t\t\"productType\": \"順豐標快\",\n" +
                "\t\t\"collectMoney\": \"\",\n" +
                "\t\t\"transPrice\": 22,\n" +
                "\t\t\"receiptTime\": \"\",\n" +
                "\t\t\"packageNumber\": 0,\n" +
                "\t\t\"taxMoney\": \"\",\n" +
                "\t\t\"taxPayment\": \"\",\n" +
                "\t\t\"isPrintStubLogo\": \"true\",\n" +
                "\t\t\"declaredValue\": \"\",\n" +
                "\t\t\"reffNo\": \"\",\n" +
                "\t\t\"fromCountryCode\": \"\",\n" +
                "\t\t\"toCountryCode\": \"852\",\n" +
                "\t\t\"customLogoBase64\": \"\",\n" +
                "\t\t\"signReturn\": \"\",\n" +
                "\t\t\"insurePrice\": \"\",\n" +
                "\t\t\"transPriceUnit\": \"HKD\",\n" +
                "\t\t\"declaredValueUnit\": \"\",\n" +
                "\t\t\"languageTC\": \"true\"\n" +
                "\t}]\n" +
                "}";*/

   /*     {
            "cargoDetails":[
            {
                "count":2.365,
                    "unit":"个",
                    "weight":6.1,
                    "amount":100.5111,
                    "currency":"HKD",
                    "name":"护肤品1",
                    "sourceArea":"CHN"
            }],
            "contactInfoList":	[
            {
                "address":"广东省深圳市南山区软件产业基地11栋",
                    "contact":"小曾",
                    "contactType":1,
                    "country":"CN",
                    "postCode":"580058",
                    "tel":"4006789888"
            },
            {
                "address":"广东省广州市白云区湖北大厦",
                    "company":"顺丰速运",
                    "contact":"小邱",
                    "contactType":2,
                    "country":"CN",
                    "postCode":"580058",
                    "tel":"18688806057"
            }],
            "language":"zh_CN",
                "orderId":"OrderNum20200612223"
        }*/

        params.put("partnerID", CLIENT_CODE);  // 顾客编码 ，对应丰桥上获取的clientCode
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        params.put("serviceCode", standardService.getCode());// 接口服务码
        params.put("timestamp", timeStamp);
        params.put("msgData", msgData);
        params.put("msgDigest", tools.getMsgDigest(msgData, timeStamp, CHECK_WORD));

        long startTime = System.currentTimeMillis();

        System.out.println("====调用实际请求：" + params);
        String result = HttpClientUtil.post(CALL_URL_BOX, params);

        //   System.out.println("====调用丰桥的接口服务代码：" + String.valueOf(standardService.getCode()) + " 接口耗时："+ String.valueOf(System.currentTimeMillis()-startTime)+"====");
        System.out.println("===调用地址 ===" + CALL_URL_BOX);
        //   System.out.println("===顾客编码 ==="+CLIENT_CODE);
        System.out.println("===返回结果：" + result);

    }

    //面单打印接口
    @PostMapping("/printsheet")
    @ResponseBody
    public Map<String, Object> postExpressDelivery(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        String msgData = httpServletRequest.getParameter("msgData");
        Map<String, Object> resMap = expressDeliveryService.PrintSheet(msgData);

        return resMap;
    }

    //下单是否成功接口
    @PostMapping("/placeOrderResult")
    @ResponseBody
    public Map<String, Object> placeOrderResult(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        String msgData = httpServletRequest.getParameter("msgData");
        Map<String, Object> resMap = expressDeliveryService.placeOrderResult(msgData);

        return resMap;
    }

    //查询订单接口
    @PostMapping("/selectOrder")
    @ResponseBody
    public Map<String, Object> selectOrder(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        String saleordernum = httpServletRequest.getParameter("saleordernum");

        Map<String, Object> resMap = expressDeliveryService.selectOrder(saleordernum);
        return resMap;
    }

    @PostMapping("/selectInformation")
    @ResponseBody
    public List<ExpressDelivery> selectInformation(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        String saleordernum = httpServletRequest.getParameter("saleordernum");
        List<ExpressDelivery> list = expreeDeliveryMapper.selectInformation(saleordernum);

        return list;
    }

    //查询运费接口
    @PostMapping("/selectMoney")
    @ResponseBody
    public Map<String, Object> selectMoney(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        String msgData = httpServletRequest.getParameter("msgData");
        Map<String, Object> resMap = expressDeliveryService.selectOrder(msgData);
        return resMap;
    }

    @PostMapping("/insert")
    @ResponseBody
    public void insert(ExpressDelivery expressDelivery) throws Exception {

        String loginname = SecurityUtils.getLoginUser().getNickName();
        expressDelivery.setLoginname(loginname);
        expressDeliveryService.insert(expressDelivery);
    }

}
